package com.yaoxiaowen.project2.test.database;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yaoxiaowen.project2.R;
import com.yaoxiaowen.project2.base.BaseActivity;
import com.yaoxiaowen.project2.test.database.test_realm.DogBean;
import com.yaoxiaowen.project2.test.database.test_realm.PersonBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmList;
import io.realm.RealmResults;

public class DbMainActivity extends BaseActivity {






    @BindView(R.id.dbTv1)
    TextView dbTv1;
    @BindView(R.id.dbTv2)
    TextView dbTv2;
    @BindView(R.id.dbTv3)
    TextView dbTv3;
    @BindView(R.id.dbTv4)
    TextView dbTv4;

//    DogBean dog1;
//    DogBean dog2;
//    DogBean dog3;
//    Realm realm;
//    RealmResults<DogBean> puppies;
//
//    DogBean manageDog;

    public static final String TAG = "DbMainActivity";
    private static final boolean DEBUG = true;
    private static final boolean IS_SHOW = true;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_db_main;
    }

    @Override
    protected void initData() {
//        realm = Realm.getDefaultInstance();
//
//        dog1 = new DogBean("dog1", 1);
//        dog1 = new DogBean("dog2", 2);
//        dog1 = new DogBean("dog3", 3);
//
//        puppies = realm.where(DogBean.class).lessThan("age", 2).findAll();
//        puppies.size();
//        mLog.i(DEBUG, TAG, "initData(), puppies.size = " + puppies.size());
//        mShow.showToast(IS_SHOW, "initData(), puppies.size = " + puppies.size());
//        dbTv1.setText("initData(), puppies.size = " + puppies.size());
//
//        puppies.addChangeListener(new RealmChangeListener<RealmResults<DogBean>>() {
//            @Override
//            public void onChange(RealmResults<DogBean> element) {
//                mLog.i(DEBUG, TAG, "initData(), changeListener -->  puppies.size = " + puppies.size());
//                mShow.showToast(IS_SHOW, "initData(), hangeListener --> puppies.size = " + puppies.size());
//
//                dbTv2.setText("initData(), hangeListener --> puppies.size = " + puppies.size());
//            }
//        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.dbBtn1)
    public void btn1OnClick(View view){
        //存储数据

        int pNum = 5;
        int dogNum = 3;
        int dogAgeCount = 1;
        List<PersonBean> personList = new ArrayList<>();
        for (int i=0; i<pNum; i++){
            RealmList<DogBean> dogList = new RealmList<>();
            for (int j=0;j<dogNum; j++){
                DogBean dog = new DogBean("name:" + dogAgeCount, dogAgeCount);
                dogList.add(dog);
            }
            PersonBean person = new PersonBean("peopleName-" + i, dogList);
            personList.add(person);
        }

        StringBuilder sb = new StringBuilder();
        sb.append("数据初始化完成 \t "  + personList);
        mLog.i(DEBUG, TAG, sb.toString());
        dbTv1.setText(sb.toString());


        Realm realm = Realm.getDefaultInstance();
        realm.beginTransaction();

        realm.copyToRealm(personList);

        realm.commitTransaction();
//        realm.beginTransaction();
//        manageDog = realm.copyToRealm(dog1);
//        PersonBean person = realm.createObject(PersonBean.class);
//
//        person.getDogs().add(manageDog);
//        mLog.i(DEBUG, TAG, "btn1OnClick() realm startCommit()");
//        realm.commitTransaction();
//
//
//        puppies.addChangeListener(new RealmChangeListener<RealmResults<DogBean>>() {
//            @Override
//            public void onChange(RealmResults<DogBean> element) {
//                mLog.i(DEBUG, TAG, "btn1 click, changeListener -->  puppies.size = " + puppies.size());
//                mShow.showToast(IS_SHOW, "btn1 click, hangeListener --> puppies.size = " + puppies.size());
//
//                dbTv4.setText("btn1 click, hangeListener --> puppies.size = " + puppies.size());
//            }
//        });
    }

    @OnClick(R.id.dbBtn2)
    public void btn2OnClick(View view){
        //取出数据
        Realm realm = Realm.getDefaultInstance();
        List<PersonBean> personList = realm.where(PersonBean.class).findAll();
        mLog.i(DEBUG, TAG, "得到数据 ->"  + personList);
        dbTv2.setText( "得到数据 ->"  + personList );
//        realm.executeTransactionAsync(new Realm.Transaction() {
//            @Override
//            public void execute(Realm bgRealm) {
//                mLog.i(DEBUG, TAG, "btn 222  开始设置 数据");
//                DogBean dog = bgRealm.where(DogBean.class).equalTo("age", 1).findFirst();
//
//                if (dog != null){
//                    dog.setAge(3);
//                }else {
//                    mLog.i(DEBUG, TAG, "dog == null");
////                    dbTv4.setText("dog == null");
//                }
//
//
//                mLog.i(DEBUG, TAG, "btn 222  设置数据 结束");
//
//            }
//        }, new Realm.Transaction.OnSuccess(){
//            @Override
//            public void onSuccess() {
//                StringBuilder sb = new StringBuilder();
//                sb.append("btn 222 onclick \t");
//                sb .append(" puppies.size() = " +  puppies.size());
//                sb.append("\t manageDog.getAge() = " + manageDog.getAge());
//
//                mLog.i(DEBUG, TAG, sb.toString());
//                mShow.showToast(IS_SHOW, sb.toString());
//                dbTv3.setText(sb.toString());
//            }
//        });
    }

    @OnClick(R.id.dbBtn3)
    public void btn3OnClick(View view){

    }

    @OnClick(R.id.dbBtn4)
    public void btn4OnClick(View view){

    }

    @OnClick(R.id.dbBtn5)
    public void btn5OnClick(View view){

    }

}
